package com.woniuxy.charin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.woniuxy.charin.model.vo.RechargeRecordVo;
import com.woniuxy.charin.service.RechargeRecordService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Api(tags = "充值管理")
@RestController
@RequestMapping("/rechargeRecord")
public class RechargeRecordController {

    @Autowired
    private RechargeRecordService rechargeRecordService;



    @PostMapping("/rechargeRecords")
    public ResponseEntity<Map<String, Object>> getRechargeRecords(@RequestBody Map<String, Object> params) {
        // 提取分页参数，设置默认值
        int pageNum = params.get("pageNum") != null ? Integer.parseInt(params.get("pageNum").toString()) : 1;
        int pageSize = params.get("pageSize") != null ? Integer.parseInt(params.get("pageSize").toString()) : 10;

        // 创建查询参数 Map（仅保留非空参数）
        Map<String, Object> queryParams = new HashMap<>();
        if (params.get("orderNo") != null && !params.get("orderNo").toString().isEmpty()) {
            queryParams.put("orderNo", params.get("orderNo").toString());
        }
        if (params.get("customerName") != null && !params.get("customerName").toString().isEmpty()) {
            queryParams.put("customerName", params.get("customerName").toString());
        }
        if (params.get("phoneNumber") != null && !params.get("phoneNumber").toString().isEmpty()) {
            queryParams.put("phoneNumber", params.get("phoneNumber").toString());
        }
        if (params.get("transactionStatus") != null && !params.get("transactionStatus").toString().isEmpty()) {
            queryParams.put("transactionStatus", params.get("transactionStatus").toString());
        }

        // 调用服务层进行查询
        Page<RechargeRecordVo> rechargeRecords = rechargeRecordService.getRechargeRecords(queryParams, pageNum, pageSize);

        // 封装返回数据
        Map<String, Object> result = new HashMap<>();
        result.put("records", rechargeRecords.getRecords());
        result.put("total", rechargeRecords.getTotal());
        result.put("current", rechargeRecords.getCurrent());
        result.put("size", rechargeRecords.getSize());

        return ResponseEntity.ok(result);
    }


    @GetMapping("/findById/{id}")
    public RechargeRecordVo getRechargeRecordById(@PathVariable("id") Long id) {
        return rechargeRecordService.getRechargeRecordById(id);
    }

}